package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.NoticeListAdapter;
import com.eightywork.life.adapter.SelectNeighbourListAdapter;
import com.eightywork.life.model.Notice;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.RTPullListView;
import com.eightywork.life.widget.RTPullListView.OnRefreshListener;
import com.eightywork.life.widget.RTPullListView.OnScrollStopAndEndListener;
import com.umeng.analytics.MobclickAgent;

public class SelectNeighbourActivity extends BaseActivity implements OnClickListener {
	private ImageButton backButton;
	private RTPullListView listView;
	public static SelectNeighbourActivity activity;
	private TextView headerTitle;
	private boolean isLoading, isRefresh = false;
	private AsyncTask<?, ?, ?> task;
	private SelectNeighbourListAdapter adapter;
	public static List<Space> list = new ArrayList<Space>();
	private int page = 1;
	private ViewGroup footView;
	private String group = "0";
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_select_neighbour_list);
		activity = this;
		initView();
		initData();
	}

	@Override
	public void onDestroy() {
		if (task != null)
			task.cancel(true);
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}
	
	private void initView() {
		
		headerTitle = (TextView) findViewById(R.id.header_title);
		backButton = (ImageButton) findViewById(R.id.button_back);
		listView = (RTPullListView) findViewById(R.id.listView_select_neighbour);
		footView = (ViewGroup) getLayoutInflater().inflate(R.layout.item_footer, null);
		backButton.setOnClickListener(this);
		
	}
	
	private void initData() {
		initHeader("选择邻居");
		adapter = new SelectNeighbourListAdapter(context, list, activity);
		listView.setAdapter(adapter);
		listView.setonRefreshListener(new OnRefreshListener() {		
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				page = 1;
				isRefresh = true;
				getNeighbourList();
				
			}
		});
		listView.setOnScrollStopAndEndListener(new OnScrollStopAndEndListener() {		
			@Override
			public void onEndAndStopScroll() {
				// TODO Auto-generated method stub
				if (!isLoading) {
					getNeighbourList();
				}
			}
		});
		listView.goLoading();
	}
	
	private void getNeighbourList() {
		task = new FreindListTask().execute();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if ( v == backButton) {
			finish();
		}
	}
	
	class FreindListTask extends AsyncTask<Void, Void, String> {
		private List<Space> aList;

		@Override
		protected void onPreExecute() {
			isLoading = true;
			if(!isRefresh){
		//		listView.addFooterView(footView);
			}
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			try {
				aList = SpaceService.friend(group, page + "", "");
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result == null) {
				if(isRefresh){
					isRefresh = false;
					list.clear();
					listView.onRefreshComplete();
				}
		//		listView.removeFooterView(footView);
				
				if (aList != null) {
					list.addAll(aList);
					adapter.notifyDataSetChanged();
				}
			} else {
				showMessage(result);
			}
			isLoading = false;
			page++;
			super.onPostExecute(result);
		}
	}
}
